﻿/**
 * @description    : 回到页面顶部
 * @author         : laohu@staff.sina.com.cn
 * @create date    : 2012-08-27
 * @change date    :  
 * @change details : 
 * @parameter      : 
 * @details        : 
 * @return         :  */

YOUXIXIU = window.YOUXIXIU || {};
(function($){
	var tmpl = '<a class="Indicator btn wbtn MST-common-goTop"  href="javascript:void(0);" id="elevator"><strong>回到<br>顶部</strong><span></span></a>';
	
	/*获取浏览器可见区域的宽高*/
	function getVisibleOffset(){
		return {
			width : window.innerWidth || document.documentElement.clientWidth,
			height : window.innerHeight || document.documentElement.clientHeight
		}
	}
	
	/*获取滚动条滚动的高度*/
	function getScrollTop(){
		return  document.documentElement.scrollTop || document.body.scrollTop;
	}
	
	function setScrollTop(p){
		var p = p || getScrollTop();
		if(document.documentElement.scrollTop){
			document.documentElement.scrollTop = p;
		}else if(document.body.scrollTop){
			document.body.scrollTop = p;
		}
	}
	
	function GotoTop(){
		this.init();
	}
	
	GotoTop.prototype = {
		contructor : GotoTop,
		init : function(){
			var $gototop = $(tmpl).appendTo($("body"));
			$(".MST-common-goTop").click(function(){
				var count = 0;
				var timer = setInterval(function(){
					var p = getScrollTop();
					if(getScrollTop() > 0){
						setScrollTop(p * Math.sin(count * Math.PI / 36));
						count++;
					}else{
						clearInterval(timer);
					}
				},100);
				return false;
			});
			
			
			var $height = $gototop.height() + 20;
			
			if($.browser.msie && $.browser.version=="6.0"){
				$gototop.css({"position":"absolute","top":(getVisibleOffset().height + getScrollTop() - $height) + "px"});
			}
			
			$gototop.css({
				marginLeft : ($(window).width() - 950 ) / 2 + 950 + 10		 
			});
			
			$(window).bind("scroll resize", function(){
				setTimeout(function(){
					if(getScrollTop()<5){
						$gototop.hide();
					}else{
						$gototop.show();
						if($.browser.msie && $.browser.version=="6.0"){
							setTimeout(function(){
								$gototop.css({"top":(getVisibleOffset().height + getScrollTop() - $height) + "px"});					
							}, 0.2 * 1000);						
						}
					}
					
					$gototop.css({
						marginLeft : ($(window).width() - 950 ) / 2 + 950 + 10		 
					});					
			}, 100);								  
			});
		}
		
	}
	
	YOUXIXIU.GOTOTOP = function(){
		return new GotoTop();
	}
})(jQuery);